//
//  WetlandService.m
//  homeland
//
//  Created by SunShine on 14-1-23.
//  Copyright (c) 2014年 xzysun. All rights reserved.
//

#import "WetlandService.h"
#import "UserInfoService.h"

@implementation WetlandService

-(void)getTicketsList:(void (^)(NSArray *data))success Failure:(void (^)(NSString *msg))failure
{
    MKNetworkEngine *currentEngine = [[MKNetworkEngine alloc] initWithHostName:WETLAND_TICKETS_LIST customHeaderFields:nil];
    MKNetworkOperation *op = [currentEngine operationWithPath:@"" params:nil httpMethod:@"POST"];
    [op addCompletionHandler:^(MKNetworkOperation *operation) {
        
        NSArray *json = [operation responseJSON];
        //转换对象,添加到列表中
        //执行回调
        success(json);
    }errorHandler:^(MKNetworkOperation *errorOp, NSError* err) {
        NSLog(@"MKNetwork request error : %@", [err localizedDescription]);
        //执行回调
        failure(@"请求门票信息失败.");
    }];
    [currentEngine enqueueOperation:op];
}

-(void)commitOrderWithCount:(NSInteger)count Tickets:(NSArray *)list Name:(NSString *)name Phone:(NSString *)phone PreseTime:(NSString *)preseTime Success:(void (^)(NSString *presellid))success Failure:(void (^)(NSString *msg))failure
{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setValue:[NSNumber numberWithInteger:count] forKey:@"allHuman"];
    [params setValue:getDeviceIdentifier() forKey:@"imeiCode"];
    [params setValue:list forKey:@"list"];
    [params setValue:name forKey:@"name"];
    [params setValue:phone forKey:@"phone"];
    [params setValue:preseTime forKey:@"preseTime"];
    [params setValue:[UserInfoService getUserInfoService].userInfo.userName forKey:@"userName"];
    [params setValue:[UserInfoService getUserInfoService].userInfo.mobile forKey:@"userPhone"];
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:params options:(NSJSONWritingOptions)0 error:nil];
    NSString *json = [[NSString alloc] initWithBytes:[jsonData bytes] length:jsonData.length encoding:NSUTF8StringEncoding];
    MKNetworkEngine *currentEngine = [[MKNetworkEngine alloc] initWithHostName:WETLAND_BOOKING_COMMIT_URL customHeaderFields:nil];
    MKNetworkOperation *op = [currentEngine operationWithPath:nil params:[NSDictionary dictionaryWithObject:json forKey:@"orderText"] httpMethod:@"POST"];
    [op addCompletionHandler:^(MKNetworkOperation *operation) {
        
        NSDictionary *json = [operation responseJSON];
        BOOL flag = [[json objectForKey:@"isSuccess"] boolValue];
        if (flag) {
            success([json objectForKey:@"presellid"]);
        } else {
            failure([json objectForKey:@"message"]);
        }
    }errorHandler:^(MKNetworkOperation *errorOp, NSError* err) {
        NSLog(@"MKNetwork request error : %@", [err localizedDescription]);
        //执行回调
        failure(@"预定门票信息失败.");
    }];
    [currentEngine enqueueOperation:op];
}

-(void)getOrderDetailForId:(NSString *)presellid Success:(void (^)(NSArray *data))success Failure:(void (^)(NSString *msg))failure
{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setValue:presellid forKey:@"id"];
    MKNetworkEngine *currentEngine = [[MKNetworkEngine alloc] initWithHostName:WETLAND_ORDER_DETAIL_URL customHeaderFields:nil];
    MKNetworkOperation *op = [currentEngine operationWithPath:nil params:params httpMethod:@"POST"];
    [op addCompletionHandler:^(MKNetworkOperation *operation) {
        
        NSArray *json = [operation responseJSON];
        success(json);
    }errorHandler:^(MKNetworkOperation *errorOp, NSError* err) {
        NSLog(@"MKNetwork request error : %@", [err localizedDescription]);
        //执行回调
        failure(@"获取订单详情失败.");
    }];
    [currentEngine enqueueOperation:op];
}

-(void)getOrderListForKey:(NSString *)presellid PagetNo:(NSInteger)page Success:(void (^)(NSDictionary *data))success Failure:(void (^)(NSString *msg))failure
{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    if ([UserInfoService getUserInfoService].currentLoginUserName) {
        [params setValue:[UserInfoService getUserInfoService].currentLoginUserName forKey:@"userName"];
    } else {
        [params setValue:getDeviceIdentifier() forKey:@"imeiCode"];
    }
    [params setValue:presellid forKey:@"presellid"];
    MKNetworkEngine *currentEngine = [[MKNetworkEngine alloc] initWithHostName:WETLAND_ORDER_LIST_URL customHeaderFields:nil];
    MKNetworkOperation *op = [currentEngine operationWithPath:[NSString stringWithFormat:@"%d", page] params:params httpMethod:@"POST"];
    [op addCompletionHandler:^(MKNetworkOperation *operation) {
        
        NSDictionary *json = [operation responseJSON];
        success(json);
    }errorHandler:^(MKNetworkOperation *errorOp, NSError* err) {
        NSLog(@"MKNetwork request error : %@", [err localizedDescription]);
        //执行回调
        failure(@"获取订单列表失败.");
    }];
    [currentEngine enqueueOperation:op];
}


//取得剩余票数
-(void)getTicketsCount:(NSString *)dateSimple  Success:(void (^)(NSString *data))success Failure:(void (^)(NSString *msg))failure
{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setValue:dateSimple forKey:@"dateSimple"];//str参数值
    
    MKNetworkEngine *currentEngine = [[MKNetworkEngine alloc] initWithHostName: WETLAND_ORDER_LIST_COUNT_URL customHeaderFields:nil];
    MKNetworkOperation *op = [currentEngine operationWithPath:nil params:params httpMethod:@"POST"];
    [op addCompletionHandler:^(MKNetworkOperation *operation) {
        
        NSDictionary *json = [operation responseJSON];
        BOOL flag = [[json objectForKey:@"isSuccess"]boolValue];
        if (flag) {
            success([json objectForKey:@"remainingTickets"]);
        } else {
            failure([json objectForKey:@"message"]);
        }
        //执行回调
    }errorHandler:^(MKNetworkOperation *errorOp, NSError* err) {
        NSLog(@"MKNetwork request error : %@", [err localizedDescription]);
        //执行回调
        failure(@"请求门票信息失败.");
    }];
    [currentEngine enqueueOperation:op];
}



//根据订单id,imeiCode and 创建时间，取得id
-(void)getTicketsOrderId:(NSString *)createTime setPresellid:(NSString*)presellid  Success:(void (^)(NSString *data))success Failure:(void (^)(NSString *msg))failure
{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setValue:presellid forKey:@"presellid"];
    [params setValue:createTime forKey:@"createTime"];
    [params setValue:getDeviceIdentifier() forKey:@"imeiCode"];
    MKNetworkEngine *currentEngine = [[MKNetworkEngine alloc] initWithHostName: WETLAND_ORDER_INFO_GET_ID_URL customHeaderFields:nil];
    MKNetworkOperation *op = [currentEngine operationWithPath:nil params:params httpMethod:@"POST"];
    [op addCompletionHandler:^(MKNetworkOperation *operation) {
        
        NSDictionary *json = [operation responseJSON];
        NSLog(@"请求id = %@",[json objectForKey:@"id"]);
        success([json objectForKey:@"id"]);
    }errorHandler:^(MKNetworkOperation *errorOp, NSError* err) {
        NSLog(@"MKNetwork request error : %@", [err localizedDescription]);
        //执行回调
        failure(@"请求失败.");
    }];
    [currentEngine enqueueOperation:op];
}
//删除记录。
-(void)orderDelete:(NSString *)deleteId   Success:(void (^)(NSString *data))success Failure:(void (^)(NSString *msg))failure{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setValue:deleteId forKey:@"id"];

    MKNetworkEngine *currentEngine = [[MKNetworkEngine alloc] initWithHostName: WETLAND_ORDER_INFO_DELETE_ID_URL customHeaderFields:nil];
    MKNetworkOperation *op = [currentEngine operationWithPath:nil params:params httpMethod:@"POST"];
    [op addCompletionHandler:^(MKNetworkOperation *operation) {
        
        NSDictionary *json = [operation responseJSON];
  
        success([json objectForKey:@"isSuccess"]);
    }errorHandler:^(MKNetworkOperation *errorOp, NSError* err) {
        NSLog(@"MKNetwork request error : %@", [err localizedDescription]);
        //执行回调
        failure(@"请求失败.");
    }];
    [currentEngine enqueueOperation:op];
}
@end
